<!DOCTYPE html>
<meta charset="UTF-8">
<form id="myselecttype" style="margin-top: 20px">
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">类型：</label>
			<div class="layui-input-inline">
				<input type="text" name="type" id="type" class="layui-input"placeholder="请输入类型"/>
			</div>
		</div>
		

		<div class="layui-inline">
			<div class="layui-btn-group ">
				<button type="button" class="layui-btn layui-icon layui-icon-search"onclick="searchtype()">查询</button>
				<button type="button" class="layui-btn layui-icon layui-icon-circle"onclick="emptytype()">清空</button>
			</div>
		</div>
	</div>
</form>
<script type="text/html" id="toolbar">
	<div class="layui-btn-group ">
   		<button class="layui-btn layui-icon layui-icon-add-1" lay-event="add"> 新增</button>
  	  	<button class="layui-btn layui-icon layui-icon-edit" lay-event="update"> 修改</button>
  	  	<button class="layui-btn layui-icon layui-icon-delete" lay-event="delete"> 删除</button>
	</div>
</script>
<table id="type_table" lay-filter="test"></table>
<script>
layui.use('table', function(){
  var table = layui.table;
  table.render({
	  	title:'学生列表',
	  	color:'#666',
		height: 'auto',
	    elem: '#type_table',
	    url:'bookType/page',
	    toolbar: '#toolbar',
	    page: true,
	    cols: [
	    	[
	      {type:'radio'},
	      {field:'type', width:'90%', title: '类型'}
	    ]
	    	]
  });
  table.on('toolbar',function(obj){
  	switch(obj.event){
      	case 'add':
      		add();
      		break;
      	case 'update':
      		update();
      		break;
      	case 'delete':
      		del();
      		break;
  	}
  });
  //新增
  function add(){
		layer.open({
	   	type:2,
	   	title:'<i class="layui-icon">新增</i>',
	   	area: ['500px','500px'],
	   	content:['bookType/edit'],
	   	btn:['提交','退出'],
	   	yes : function(index, layero) {
			var obj = layero.find("iframe")[0].contentWindow;
			var win = top.window['layui-layer-iframe' + index];
			var type = win.$("#type").val();
			$.post('bookType/save', {type:type}, function(msg) {
				layer.msg(msg);
				red();
			});
			layer.closeAll('iframe');
		
		}
	  })
  }
  
  function update(){
	  var checkStatus = table.checkStatus('type_table');
	  var data = checkStatus.data;
	  var zid = data.length==1?data[0].id: '';
	  if(data.length!=1){
		  layer.msg("请选择一行进行修改！");
		  return;
	  }
	  layer.open({
	    	type:2,
	    	title:'<i class="layui-icon">修改</i>',
	    	area: ['500px','500px'],
	    	content:['bookType/edit?id='+zid],
	    	btn:['提交','退出'],
	    	yes : function(index, layero) {
				var obj = layero.find("iframe")[0].contentWindow;
				var win = top.window['layui-layer-iframe' + index];
				var id = win.$("#id").val();
				var type = win.$("#type").val();
				$.post('bookType/save', {id:id,type:type}, function(msg) {
					layer.msg(msg);
					red();
				});
				layer.closeAll('iframe');
			}
		  })
  }
  
  function del(){
	  var checkStatus = table.checkStatus('type_table');
	  var data = checkStatus.data;
	  var zid = data.length==1?data[0].id: '';
	  if(data.length!=1){
		  layer.msg("请选择一行进行删除！");
		  return;
	  }
	  layer.confirm('确定删除此行数据吗？', function() {
		  $.post('bookType/delete',{id:zid},function(result){
			  layer.msg(result);
			  red();
		  })
	  });
  }
  
  });
//刷新表格
function red(){
	var table = layui.table;
  	table.reload('type_table', {
	  url: 'bookType/page'
	});
}
//查询
function searchtype(){
	var type = $('#type').val();
	layui.use('table', function(){
		var table = layui.table;
		table.reload('type_table',{
			url:'bookType/page',
			where: {
				type: type
			}
		});
	});
}
//清空表格
function emptytype(){
	$("#myselecttype")[0].reset();
	var type = $('#type').val();
	layui.use('table', function(){
		var table = layui.table;
	  	table.reload('type_table', {
		  url: 'bookType/page',
		  where: {
			  type: type
			}
		});
	});
}
  </script>